﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:toms="urn:TomsToolbox"
                    xmlns:styles="clr-namespace:tomenglertde.ResXManager.Styles">

  <Visibility x:Key="Visible">Visible</Visibility>

  <Style x:Key="ComboBoxToggleButtonStyle" TargetType="{x:Type ToggleButton}">
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="Focusable" Value="False" />
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="MinWidth" Value="18" />
    <Setter Property="Cursor" Value="Hand" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="ToggleButton">
          <Grid Background="Transparent">
            <Border Width="18" HorizontalAlignment="Right" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
              <Path Width="10" Height="6" VerticalAlignment="Center"
                    StrokeThickness="2" SnapsToDevicePixels="True"
                    Data="M 0 0 L 3.5 4 L 7 0 Z"
                    Stroke="{TemplateBinding Foreground}" Fill="{TemplateBinding Foreground}" />
            </Border>
          </Grid>
          <ControlTemplate.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
              <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" />
            </Trigger>
            <Trigger Property="IsPressed" Value="True">
              <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" />
            </Trigger>
            <Trigger Property="IsChecked" Value="True">
              <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
              <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style x:Key="ComboBoxItemStyle" TargetType="{x:Type ComboBoxItem}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="VerticalAlignment" Value="Stretch" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="HorizontalContentAlignment" Value="Left" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="Padding" Value="3,2" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Margin" Value="0" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="ComboBoxItem">
          <Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}">
            <ContentPresenter />
          </Border>
          <ControlTemplate.Triggers>
            <Trigger Property="IsHighlighted" Value="True">
              <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" />
            </Trigger>
            <Trigger Property="IsSelected" Value="True">
              <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
              <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
            </Trigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <ControlTemplate x:Key="ComboBoxEditableTemplate" TargetType="{x:Type ComboBox}">
    <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
      <Grid SnapsToDevicePixels="True">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Popup x:Name="PART_Popup" Grid.Column="0" Grid.ColumnSpan="2" AllowsTransparency="True"
               IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}"
               Placement="Bottom">
          <Border x:Name="DropDownBorder"
                  MinWidth="{Binding ActualWidth, ElementName=Border}"
                  MaxHeight="{TemplateBinding MaxDropDownHeight}"
                  HorizontalAlignment="Stretch"
                  Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"
                  BorderBrush="{DynamicResource {x:Static styles:ResourceKeys.BorderBrush}}"
                  BorderThickness="1" CornerRadius="0">
            <ScrollViewer x:Name="DropDownScrollViewer">
              <Grid RenderOptions.ClearTypeHint="Enabled">
                <Canvas Width="0" Height="0" HorizontalAlignment="Left" VerticalAlignment="Top">
                  <Rectangle x:Name="OpaqueRect"
                             Width="{Binding ActualWidth, ElementName=DropDownBorder}"
                             Height="{Binding ActualHeight, ElementName=DropDownBorder}"
                             Fill="{Binding Background, ElementName=DropDownBorder}" />
                </Canvas>
                <ItemsPresenter x:Name="ItemsPresenter"
                                KeyboardNavigation.DirectionalNavigation="Contained"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
              </Grid>
            </ScrollViewer>
          </Border>
        </Popup>
        <Grid Margin="{TemplateBinding Padding}" Grid.Column="0" VerticalAlignment="Center">
          <TextBox x:Name="PART_EditableTextBox" 
                 HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                 VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                 BorderThickness="0" Padding="0" Background="Transparent"
                 IsReadOnly="{Binding IsReadOnly, RelativeSource={RelativeSource TemplatedParent}}" />
        </Grid>
        <ToggleButton Grid.Column="1"
                      Background="{TemplateBinding Background}"
                      BorderBrush="{TemplateBinding BorderBrush}"
                      IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                      Style="{StaticResource ComboBoxToggleButtonStyle}" />
      </Grid>
    </Border>
    <ControlTemplate.Triggers>
      <Trigger Property="HasItems" Value="False">
        <Setter TargetName="DropDownBorder" Property="Height" Value="95" />
      </Trigger>
      <Trigger Property="IsGrouping" Value="True">
        <Setter Property="ScrollViewer.CanContentScroll" Value="False" />
      </Trigger>
      <Trigger SourceName="DropDownScrollViewer" Property="ScrollViewer.CanContentScroll" Value="False">
        <Setter TargetName="OpaqueRect" Property="Canvas.Top" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}" />
        <Setter TargetName="OpaqueRect" Property="Canvas.Left" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}" />
      </Trigger>
      <Trigger Property="IsEnabled" Value="False">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
        <Setter Property="Background" Value="{DynamicResource {x:Static styles:ResourceKeys.DisabledBrush}}" />
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>

  <ControlTemplate x:Key="ComboBoxDropDownTemplate" TargetType="{x:Type ComboBox}">
    <Border x:Name="Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
      <Grid SnapsToDevicePixels="True">
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="18" />
        </Grid.ColumnDefinitions>
        <Popup x:Name="PART_Popup" AllowsTransparency="True" Grid.Column="0" Grid.ColumnSpan="2"
               Focusable="False" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}"
               Placement="Bottom" SnapsToDevicePixels="True">
          <Border x:Name="DropDownBorder" MinWidth="{Binding ActualWidth, ElementName=Border}"
                  MaxHeight="{TemplateBinding MaxDropDownHeight}" HorizontalAlignment="Stretch"
                  Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"
                  BorderBrush="{DynamicResource {x:Static styles:ResourceKeys.BorderBrush}}"
                  BorderThickness="1,1,1,1" CornerRadius="0" Focusable="False">
            <ScrollViewer x:Name="DropDownScrollViewer">
              <Grid RenderOptions.ClearTypeHint="Enabled">
                <Canvas Width="0" Height="0" HorizontalAlignment="Left" VerticalAlignment="Top" Focusable="False">
                  <Rectangle x:Name="OpaqueRect" Width="{Binding ActualWidth, ElementName=DropDownBorder}"
                             Height="{Binding ActualHeight, ElementName=DropDownBorder}"
                             Fill="{Binding Background, ElementName=DropDownBorder}" Focusable="False" />
                </Canvas>
                <ItemsPresenter x:Name="ItemsPresenter" Focusable="False"
                                KeyboardNavigation.DirectionalNavigation="Contained"
                                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
              </Grid>
            </ScrollViewer>
          </Border>
        </Popup>
        <ToggleButton Grid.Column="0" Grid.ColumnSpan="2"
                      IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                      Style="{StaticResource ComboBoxToggleButtonStyle}" />
        <Grid Grid.Column="0" Margin="{TemplateBinding Padding}"
              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
              VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
          <ContentPresenter Content="{TemplateBinding SelectionBoxItem}"
                            ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}"
                            ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                            ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" Focusable="False"
                            IsHitTestVisible="False" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
          <Rectangle x:Name="FocusVisual" StrokeThickness="1" Visibility="Hidden"
                           Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
                           StrokeDashArray="1 2" Margin="-2,-1" SnapsToDevicePixels="True" />
        </Grid>
      </Grid>
    </Border>
    <ControlTemplate.Triggers>
      <Trigger Property="HasItems" Value="False">
        <Setter TargetName="DropDownBorder" Property="Height" Value="95" />
      </Trigger>
      <Trigger Property="IsGrouping" Value="True">
        <Setter Property="ScrollViewer.CanContentScroll" Value="False" />
      </Trigger>
      <Trigger SourceName="DropDownScrollViewer" Property="ScrollViewer.CanContentScroll" Value="False">
        <Setter TargetName="OpaqueRect" Property="Canvas.Top" Value="{Binding VerticalOffset, ElementName=DropDownScrollViewer}" />
        <Setter TargetName="OpaqueRect" Property="Canvas.Left" Value="{Binding HorizontalOffset, ElementName=DropDownScrollViewer}" />
      </Trigger>
      <MultiDataTrigger>
        <MultiDataTrigger.Conditions>
          <Condition Binding="{Binding IsKeyboardFocused, RelativeSource={RelativeSource Self}}" Value="True" />
          <Condition Binding="{Binding IsFocusVisualVisible, Source={x:Static toms:KeyboardNavigation.Current}}" Value="True" />
        </MultiDataTrigger.Conditions>
        <Setter Property="Visibility" TargetName="FocusVisual" Value="Visible" />
      </MultiDataTrigger>
      <Trigger Property="IsEnabled" Value="False">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
        <Setter Property="Background" Value="{DynamicResource {x:Static styles:ResourceKeys.DisabledBrush}}" />
      </Trigger>
    </ControlTemplate.Triggers>
  </ControlTemplate>

  <Style x:Key="{x:Static styles:ResourceKeys.ComboBoxStyle}" TargetType="{x:Type ComboBox}">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}" />
    <Setter Property="Padding" Value="6,3,0,3" />
    <Setter Property="Margin" Value="0" />
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" />
    <Setter Property="BorderBrush" Value="{DynamicResource {x:Static styles:ResourceKeys.BorderBrush}}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="ItemContainerStyle" Value="{StaticResource ComboBoxItemStyle}" />
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
    <Setter Property="ScrollViewer.CanContentScroll" Value="True" />
    <Setter Property="ScrollViewer.PanningMode" Value="Both" />
    <Setter Property="Stylus.IsFlicksEnabled" Value="False" />
    <Setter Property="Template" Value="{StaticResource ComboBoxDropDownTemplate}" />
    <Style.Triggers>
      <Trigger Property="IsEditable" Value="True">
        <Setter Property="IsTabStop" Value="False" />
        <Setter Property="Padding" Value="3,3,0,3" />
        <Setter Property="Template" Value="{StaticResource ComboBoxEditableTemplate}" />
      </Trigger>
      <Trigger Property="Validation.HasError" Value="True">
        <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}" />
      </Trigger>
      <MultiTrigger>
        <MultiTrigger.Conditions>
          <Condition Property="IsMouseOver" Value="True"/>
          <Condition Property="IsEnabled" Value="True"/>
        </MultiTrigger.Conditions>
        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" />
      </MultiTrigger>
    </Style.Triggers>
  </Style>

  <Style TargetType="ComboBox" x:Key="{x:Static styles:ResourceKeys.DataGridComboBoxEditingElementStyle}" BasedOn="{StaticResource {x:Static styles:ResourceKeys.ComboBoxStyle}}">
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="Padding" Value="1" />
    <Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
  </Style>

  <Style TargetType="ComboBox" x:Key="{x:Static styles:ResourceKeys.DataGridComboBoxElementStyle}">
    <Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
  </Style>

</ResourceDictionary>